package com.kai.steering.data.service.ctl;

import com.kai.steering.data.service.entity.AnswerResult;
import com.kai.steering.data.service.entity.domainObject.AnswerDO;
import com.kai.steering.data.service.entity.domainObject.AnswerJournalDO;
import com.kai.steering.data.service.entity.dto.AnswerDTO;
import com.kai.steering.data.service.entity.vo.AnswerVO;
import com.kai.steering.data.service.service.AnswerJournalService;
import com.kai.steering.data.service.service.AnswerService;

import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.ctl.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import javax.validation.Valid;

import com.kai.steering.common.web.vo.ReturnCodeExt;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <pre>
 * 答案 前端控制器
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@RestController
@RequestMapping("/answer")
@Api(tags = "答案 API")
public class AnswerController extends BaseController {
    @Autowired
    private AnswerService answerService;

    @Autowired
    private AnswerJournalService answerJournalService;

    @PostMapping
    @ApiOperation(value = "答案-添加")
    public Result<AnswerDO> addAnswer(@Valid @RequestBody AnswerDTO answerDTO) {

        //参数检查
        if (answerDTO.getQuestionId()==null) {
            return Result.failed("QuestionId为空");
        }
        answerDTO.setAnswerId(null);
        answerDTO.setDeleteDatetime(null);
        //构建新对象
        AnswerDO answerDO = new AnswerDO(answerDTO);

        //保存
        AnswerDO newAnswerDO = answerService.saveAnswer(answerDO);
        return Result.ok(newAnswerDO);
    }


    @PutMapping("/{id}")
    @ApiOperation(value = "答案-编辑")
    public Result<Boolean> updateAnswer(@PathVariable("id") Serializable id, @Valid @RequestBody AnswerDTO answerDTO) {

        if (answerDTO.getQuestionId()==null) {
            return Result.failed("QuestionId为空");
        }
        if (id==null) {
            return Result.failed("id为空");
        }
        AnswerDO answerDO = new AnswerDO(answerDTO);
        //修改属性
        boolean flag = answerService.updateAnswer(id,answerDO);
        return Result.ok(flag);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "答案-删除")
    public Result<Boolean> deleteAnswer(@PathVariable("id") Serializable id) {
        if (id == null) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("id不存在");
        }
        boolean b = answerService.deleteAnswer(id);
        return Result.ok(b);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "答案-详情")
    public Result<AnswerVO> getAnswer(@PathVariable("id") Serializable id) {
        if (id == null) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("id不存在");
        }
        AnswerDO answerById = answerService.getAnswerById(id);
        AnswerVO answerVO = new AnswerVO(answerById);

        return Result.ok(answerVO);
    }
    @GetMapping("/question/{id}")
    @ApiOperation(value = "答案-获取题目的答案")
    public Result<List<AnswerVO>> getQuestionAnswer(@PathVariable("id") Serializable id) {
        if (id == null) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("id不存在");
        }
        List<AnswerDO> answerByQId = answerService.getAnswerByQId(id.toString());
        List<AnswerVO> collect = answerByQId.stream().map(answerDO -> {
            return new AnswerVO(answerDO);
        }).collect(Collectors.toList());

        return Result.ok(collect);
    }
//
//
//    @PostMapping("/list")
//    @ApiOperation(value = "答案-综合查询")
//    public Result<Page<AnswerVO>> getAnswerPageList(@Valid @RequestBody AnswerQueryDTO answerQueryDTO) {
//        Page<AnswerVO> paging = null;
//        return Result.ok(paging);
//    }
//
//
//    @GetMapping("/list")
//    @ApiOperation(value = "答案-全部列表")
//    public Result<List<AnswerVO>> getAnswerAllList() {
//
//        List<AnswerVO
//                > list = null;
//        return Result.ok(list);
//    }

    @PostMapping("/replyAnswer")
    @ApiOperation(value = "答案-回答")
    public Result<AnswerResult> replyAnswer(@Valid @RequestBody AnswerDTO answerDTO) {
        /**
         * 校验参数
         */
        if (answerDTO.getQuestionId()==null||answerDTO.getAnswer()==null) {
            return Result.failed("查询参数不存在");
        }

        /**
         * 转换
         */
        AnswerDO answerDO = new AnswerDO(answerDTO);
        /**
         * 获取+
         */
        AnswerResult answerResult=answerService.replyAnswer(answerDO);




        return Result.ok(answerResult);
    }
}